import Vue from 'vue'
import store from '@/vuex/store'
import router from '@/router'
Vue.mixin({
    /**
     * 路由加载时
     */
    mounted() {
        if (this.$vnode && this.$vnode.data.keepAlive) {
            if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) {
                if (this.$vnode.componentOptions) {
                    const key = this.$vnode.key == null ? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '') : this.$vnode.key
                    const fullPath = router.currentRoute.fullPath
                    store.dispatch('ADD_CACHED_VIEW', { key: key, path: fullPath })
                }
            }
        }
    },
    /**
     * 路由离开前
     * @param {*} to 
     * @param {*} from 
     * @param {*} next 
     */
    beforeRouteLeave(to, from, next) {
        // let cachedViews = store.getters.cachedViews
        // let isClean = true
        // cachedViews.forEach(x => {
        //     if (x.path === from.fullPath) {
        //         isClean = false
        //     }
        // });
        // // 如果vuex中已没有标识缓存此页，就删除缓存
        // if (isClean) {
        //     if (this.$vnode && this.$vnode.data.keepAlive) {
        //         if (this.$vnode.parent && this.$vnode.parent.componentInstance && this.$vnode.parent.componentInstance.cache) {
        //             if (this.$vnode.componentOptions) {
        //                 var key = this.$vnode.key == null
        //                     ? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '')
        //                     : this.$vnode.key;
        //                 var cache = this.$vnode.parent.componentInstance.cache;
        //                 var keys = this.$vnode.parent.componentInstance.keys;
        //                 if (cache[key]) {
        //                     if (keys.length) {
        //                         var index = keys.indexOf(key);
        //                         if (index > -1) {
        //                             keys.splice(index, 1);
        //                         }
        //                     }
        //                     delete cache[key];
        //                 }
        //             }
        //         }
        //     }
        //     this.$destroy();
        // }
        next();
    }
})
